package action;

import storage.DataManager;
import data.Event;

public class ToggleAction extends Action
{
	enum TOGGLE_TYPE {
		TICK, STAR
	};

	private Event eventToToggle;
	private TOGGLE_TYPE type;
	//private boolean oldEventValue;
	
	public ToggleAction(TOGGLE_TYPE type) {
		this.type = type;
	}

	public Event[] execute(Event e){
		eventToToggle = e;
		boolean toggleSuccessful = toggle(DataManager.getEventById(e.getId()));
		
		if(toggleSuccessful) {
			Event[] result = new Event[1];
			result[0] = e;
			return result;
		}
		
		return null;// toggle not successful
	}
	
	public boolean toggle(Event eventToToggle) {
		if (type.equals(TOGGLE_TYPE.STAR)) {
			eventToToggle.toggleStarred();
		} else if (type.equals(TOGGLE_TYPE.TICK)) {
			eventToToggle.toggleTicked();
		}
		return DataManager.replace(eventToToggle, eventToToggle);
	}

	@Override
	public Event[] undo() {
		return execute(eventToToggle);
	}

	@Override
	public boolean isUndoable() {
		return true;
	}

	@Override
	public String errorMessage() {
		return String.format("Item could not be $1%s/un$1%s", type.toString().toLowerCase());
	}

	@Override
	public Event[] execute(String userCommand) {
		return null;
	}

	@Override
	public String getCommandName() {
		return type.name().toLowerCase();
	}
}
